<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
var preorderTraversal = function(root) {
    let stack = []
    let res = []

    if(root){
        stack.push(root)
    }
    
    while(stack.length){
        let n = stack.pop()
        res.push(n.val)
        if(n.right){
            stack.push(n.right)
        }
        if(n.left){
            stack.push(n.left)
        }
    }
    return res
};
    </script>
</body>
</html>